package com.xyz.eventconsumer;

import com.free.common.vo.ResultModel;
import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import com.xyz.blockqueue.EventQueue;
import com.xyz.blockqueue.QueueModel;
import com.xyz.eventoper.EventOperContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EventConsumer {

    private static final FreeLogger LOG = FreeLoggerFactory.getLogger(EventConsumer.class);

    @Autowired
    public EventOperContext context;

    public void init(){
        consume();
    }

    public void consume(){
        while(true){
            try {
                QueueModel model = EventQueue.getInstance().take();
                LOG.info("处理事件{}",model.getEventType().toString());
                ResultModel res = context.executeOper(model);
                if(res.isSuccess()){
                    LOG.info("处理事件{},处理成功",model.getEventType());
                }else{
                    LOG.info("策略信息{},处理失败,信息{}",model.getEventType(),res.getMessage());
                }
            } catch (Exception e) {
                LOG.info("发生异常:{}" , e);
            }
        }
    }

}
